/**
 * Created by bian on 16-3-9.
 */
var render = require('../../instances/render.js');
var auth = require('../../helpers/auth.js');
var db = require('../../models/index.js');
var debug = require('../../instances/debug');
var sequelize = require('sequelize');
var util = require('util');

module.exports = (router) => {
    router.get('/adminer/shop-info', function *() {
        var ctx = this;
        var user = yield auth.user(this);
        if(user.type !== 100){
            var data  = yield db.models.shops.findOne({
                where:{
                    id: (yield auth.user(ctx)).shopId
                },
                attributes:['mainImg','title','phone'],
                include:[{
                    model:db.models.Area,
                    attributes:['id','TopAreaId']
                }]
            });
            this.body = yield render('admin/shop-info', {
                data:data
            });
        }else{
            var data = {
                Area:{}
            };
            this.body = yield render('admin/shop-info', {
                data:data
            });
        }
    });

    router.post('/adminer/shop-info', function *(){
        var body = this.request.body;
        var ctx = this;
        this.checkBody('shopName').notEmpty();
        this.checkBody('phone').notEmpty();
        this.checkBody('TopAreaId').notEmpty();
        this.checkBody('areaId').notEmpty();

        if(this.errors){
            this.body = this.errors;
            return;
        }

        var user = yield auth.user(this);
        //普通商户管理：
        if(user.type !== 100){
            var shopName = body.shopName;
            var phone = body.phone;
            var mainImg = body.mainImg;
            yield db.models.shops.update({
                title:shopName,
                phone:phone,
                mainImg:mainImg,
                AreaId:body.areaId
            },{
                where:{
                    id: (yield auth.user(ctx)).shopId
                }
            });
            this.redirect('/adminer/shop-info');
        }

        //super用户添加商店：
        if(user.type === 100){
            yield db.models.shops.create({
                title:body.shopName,
                phone:body.phone,
                mainImg:body.mainImg,
                AreaId:body.areaId
            });
            this.redirect('/adminer-super/shops-ctrl');
        }
    });

    router.get('/adminer/shop-info/:id',function *(){
        var user = yield auth.user(this);
        if(user.type === 100){
            var data  = yield db.models.shops.findOne({
                where:{
                    id: this.params.id
                },
                attributes:['mainImg','title','phone'],
                include:[{
                    model:db.models.Area,
                    attributes:['id','TopAreaId']
                }]
            });
            this.body = yield render('admin/shop-info.html', {
                data:data
            });
        }
    });

    router.post('/adminer/shop-info/:id',function *(){
        this.checkBody('shopName').notEmpty();
        this.checkBody('phone').notEmpty();
        this.checkBody('TopAreaId').notEmpty();
        this.checkBody('areaId').notEmpty();
        var body = this.request.body;
        console.log(body);
        if(this.errors){
            this.body = this.errors;
            return;
        }
        var user = yield auth.user(this);
        if(user.type === 100){
            yield db.models.shops.update({
                title:body.shopName,
                phone:body.phone,
                mainImg:body.mainImg,
                AreaId:body.areaId
            },{
                where:{
                    id: this.params.id
                }
            });
            //var url = '/adminer/shop-info/' + this.params.id;
            this.redirect('/adminer-super/shops-ctrl');
        }
    });
};